package cn.iocoder.yudao.module.promotion.controller.admin.prizedraw;

import cn.iocoder.yudao.module.promotion.controller.admin.prizedraw.vo.PrizeDrawLotteryPageReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.prizedraw.vo.PrizeDrawLotteryRespVO;
import cn.iocoder.yudao.module.promotion.controller.admin.prizedraw.vo.PrizeDrawLotterySaveReqVO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.prizedraw.PrizeDrawLotteryDO;
import cn.iocoder.yudao.module.promotion.service.prizedraw.PrizeDrawLotteryService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 抽奖活动中奖")
@RestController
@RequestMapping("/promotion/prize-draw-lottery")
@Validated
public class PrizeDrawLotteryController {

    @Resource
    private PrizeDrawLotteryService prizeDrawLotteryService;

    @PostMapping("/create")
    @Operation(summary = "创建抽奖活动中奖")
    @PreAuthorize("@ss.hasPermission('promotion:prize-draw-lottery:create')")
    public CommonResult<Long> createPrizeDrawLottery(@Valid @RequestBody PrizeDrawLotterySaveReqVO createReqVO) {
        return success(prizeDrawLotteryService.createPrizeDrawLottery(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新抽奖活动中奖")
    @PreAuthorize("@ss.hasPermission('promotion:prize-draw-lottery:update')")
    public CommonResult<Boolean> updatePrizeDrawLottery(@Valid @RequestBody PrizeDrawLotterySaveReqVO updateReqVO) {
        prizeDrawLotteryService.updatePrizeDrawLottery(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除抽奖活动中奖")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('promotion:prize-draw-lottery:delete')")
    public CommonResult<Boolean> deletePrizeDrawLottery(@RequestParam("id") Long id) {
        prizeDrawLotteryService.deletePrizeDrawLottery(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除抽奖活动中奖")
                @PreAuthorize("@ss.hasPermission('promotion:prize-draw-lottery:delete')")
    public CommonResult<Boolean> deletePrizeDrawLotteryList(@RequestParam("ids") List<Long> ids) {
        prizeDrawLotteryService.deletePrizeDrawLotteryListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得抽奖活动中奖")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('promotion:prize-draw-lottery:query')")
    public CommonResult<PrizeDrawLotteryRespVO> getPrizeDrawLottery(@RequestParam("id") Long id) {
        PrizeDrawLotteryDO prizeDrawLottery = prizeDrawLotteryService.getPrizeDrawLottery(id);
        return success(BeanUtils.toBean(prizeDrawLottery, PrizeDrawLotteryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得抽奖活动中奖分页")
    @PreAuthorize("@ss.hasPermission('promotion:prize-draw-lottery:query')")
    public CommonResult<PageResult<PrizeDrawLotteryRespVO>> getPrizeDrawLotteryPage(@Valid PrizeDrawLotteryPageReqVO pageReqVO) {
        PageResult<PrizeDrawLotteryDO> pageResult = prizeDrawLotteryService.getPrizeDrawLotteryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PrizeDrawLotteryRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出抽奖活动中奖 Excel")
    @PreAuthorize("@ss.hasPermission('promotion:prize-draw-lottery:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPrizeDrawLotteryExcel(@Valid PrizeDrawLotteryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PrizeDrawLotteryDO> list = prizeDrawLotteryService.getPrizeDrawLotteryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "抽奖活动中奖.xls", "数据", PrizeDrawLotteryRespVO.class,
                        BeanUtils.toBean(list, PrizeDrawLotteryRespVO.class));
    }

}